package hhq.demo.wechart.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;

import hhq.demo.wechart.dao.UserDao;
import hhq.demo.wechart.popj.ChartUser;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/wechart")
public class UserController {

    static int glabol_ID=0;

    @Autowired
    UserDao userDao;
 
    /**
     * 登录用户，返回 succ 存在、no不存在、 errpwd 密码错误
     */
    @RequestMapping(value="/login", method = RequestMethod.POST,  consumes = "multipart/form-data")
    public String login(
            HttpServletRequest request,
            HttpServletResponse resp,
            @RequestParam("name") String name,
            @RequestParam("pwd") String pwd
    ) throws IOException {
        ChartUser user = userDao.queryOne(name);

        JSONObject json=new JSONObject(); // res 和 url

        if(user == null){
            json.put("res","no");
        }else if(user.getPwd().equals(pwd) ){
            String url = (String)request.getSession().getAttribute("next_url");
            request.getSession().setAttribute("user", user);
            // 跳去URL或者首页
            if(url != null){
                json.put("url",url);
                request.getSession().removeAttribute("next_url");
            }
            json.put("res","succ");
        }else{
            json.put("res","errpwd");
        }
        return json.toJSONString();
    }

    /**
     * 退出登录，返回 Y 或者 N ，清空session中的user
     */
    @RequestMapping(value="/logout", method = RequestMethod.GET)
    public String logout(
            HttpServletRequest request
    ) throws IOException {
        ChartUser user = (ChartUser)request.getSession().getAttribute("user");

      if(user!=null){
          request.getSession().removeAttribute("user");
          return "Y";
      }else{
          return "N";
      }
    }

    /**
     * 查询已经登录的用户的信息
     */
    @RequestMapping(value="/get_my_info", method = RequestMethod.GET)
    public String getUser( HttpServletRequest request   ){
        ChartUser user =  (ChartUser)request.getSession().getAttribute("user");
        JSONObject json = new JSONObject();

        if(user == null){
            json.put("res", "N");
        }else{
            json.put("res", "Y");
            json.put("uid", user.getUid());
            json.put("name", user.getName());
            json.put("brief", user.getBrief());

        }
        return json.toJSONString();
    }

    /**
     * 更新用户信息
     */
    @RequestMapping(value="/updateUser", method = RequestMethod.POST)
    public String updateUser(HttpServletRequest request,
                        @RequestParam("uid") String uid,
                        @RequestParam("name") String name,
                        @RequestParam("pwd") String pwd, 
                        @RequestParam("brief") String brief
    ){
        ChartUser user_sess =  (ChartUser)request.getSession().getAttribute("user");
        ChartUser user = userDao.queryOne(uid);

        System.out.println(uid+" " + name + pwd + brief);

        if(user == null || ! user_sess.getUid().equals(uid)){
            return "err";
        }else{
            if(!brief.isEmpty()) // 简介
                user.setBrief(brief);
            if(!name.isEmpty())  // 用户名
                user.setName(name);
            if(!pwd.isEmpty())  // 密码
                user.setPwd(pwd);
            userDao.updateOne(user); // 更新
            request.getSession().setAttribute("user", user);
        }

        return "succ";
    }


    /**
     * 用户注册，成功返回succ，错误返回err
     * http://127.0.0.1:9966/huohuaqi/wechart/register?uid=1&name=hhq&pwd=hhq
     */
    @RequestMapping(value="/register")
    public String insertUser(@RequestParam("name") String name, @RequestParam("pwd") String pwd  ){
        if(name.equals(""))
            return "err"; 
        if(  userDao.queryOne(name) == null  ){
            ChartUser user = new ChartUser();
            user.setUid("uid_" + (glabol_ID++));
            user.setName(name);
            user.setPwd(pwd);
            user.setBrief("no brief"); 
            userDao.insertOne(user); // 插入
            return "succ";
        } else{
            return "err";
        } 
    }

}